/*
 * Created on 2008-1-15
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.tongji.quickCRUDQ.dao.jdbc;

import java.util.ArrayList;
import java.util.List;

import org.acegisecurity.userdetails.UserDetails;
import org.acegisecurity.userdetails.UserDetailsService;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.springframework.dao.DataAccessException;

import edu.tongji.jiaowu.dao.hibernate.UserDaoHibernate;
import com.tongji.quickCRUDQ.dao.UserDao;
import com.tongji.quickCRUDQ.model.CommonBean;

/**
 * @author Administrator
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class UserDaoJdbc extends BaseDaoJdbc implements UserDao,UserDetailsService {
    private String tableName="APP_USER";
    private String beanName="AppUser";
    private UserDaoHibernate userDaoHibernate;

    public CommonBean createUser(){
    	return this.createBean(tableName,beanName);
    }
    
    public CommonBean findUser(String userName){
    	String sql="select * from app_user where username=?";
    	CommonBean user=this.createUser();
    	List data=this.getCommonDao().select(sql,new String[]{userName});
    	user.addData(data);
    	return user;
    }
    
    public CommonBean findUserById(String userId){
    	String sql="select * from app_user where id=?";
    	CommonBean user=this.createUser();
    	List data=this.getCommonDao().select(sql,new String[]{userId});
    	user.addData(data);
    	return user;
    }
    
	public void updatePassword(String userName, String password) {
		String sql="update app_user set password=? where username=?";
		this.getCommonDao().updateRecord(sql,new String[]{password,userName});
	}
	
    public void grantRoles(String userId,String[] roleIds){
    	String sql1="delete from  user_role where user_id=?";
    	this.getCommonDao().updateRecord(sql1,new String[]{userId});
    	String sql2="insert into user_role(user_id,role_id) values(?,?)";
        List valueArray=new ArrayList();
    	for (int i = 0; i < roleIds.length; i++) {
    		valueArray.add(new String[]{userId,roleIds[i]});
		}
    	this.getCommonDao().batchUpdateRecord(sql2,valueArray);
    }

	/* (non-Javadoc)
	 * @see org.acegisecurity.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
	 */
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
		return userDaoHibernate.loadUserByUsername(username);
    }
    
  
	/**
	 * @return Returns the userDaoHibernate.
	 */
	public UserDaoHibernate getUserDaoHibernate() {
		return userDaoHibernate;
	}
	/**
	 * @param userDaoHibernate The userDaoHibernate to set.
	 */
	public void setUserDaoHibernate(UserDaoHibernate userDaoHibernate) {
		this.userDaoHibernate = userDaoHibernate;
	}
}
